﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MLM.Data;

namespace MLM.AdminCP.Class
{
    public static class CommonSettingHelper
    {
        public static bool SaveCommonSetting(MLMDataContext ct, string key, string value)
        {
            if (ct == null)
            {
                ct = new MLMDataContext();
            }
            if (ct.CommonSettings.Count(it => it.SettingKey == key) > 0)
            {
                ct.CommonSettings.FirstOrDefault(it => it.SettingKey == key).SettingValue =
                    value;
            }
            else
            {
                ct.CommonSettings.AddObject(new CommonSetting
                {
                    SettingKey = key,
                    SettingValue = value
                });
            }
            return ct.SaveChanges() > 0;
        }
        public static string GetCommonSetting(MLMDataContext ct, string key)
        {
            if (ct == null)
            {
                ct = new MLMDataContext();
            }
            if (ct.CommonSettings.Count(it => it.SettingKey == key) > 0)
                return ct.CommonSettings.FirstOrDefault(it => it.SettingKey == key).SettingValue;
            else
            {
                return "";
            }
        }

        public static string GetName(this string ID)
        {
            long _id = long.Parse(ID);

            MLMDataContext _MLMDataContext = new MLMDataContext();
            var query = _MLMDataContext.ProductBases.Where(p => p.ProductBaseID == _id).First().Name;
            return query;
        }
        public static double GetVATByCountryUser(this object CountryID)
        {
            try
            {
                if (CountryID == null)
                {
                    return 0;
                }
                MLMDataContext _MLMDataContext = new MLMDataContext();
                long roleID = Permission.GetCurrentRole();
                double _CountryID;
                if (!string.IsNullOrEmpty(CountryID.ToString()))
                {
                    _CountryID = long.Parse(CountryID.ToString());
                    var vat = _MLMDataContext.GroupTaxes.Where(p => p.CountryID == _CountryID && roleID == roleID);

                    if (vat.Count() > 0)
                    {
                        return double.Parse(vat.FirstOrDefault().VAT.ToString());
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception)
            {
                return 0;

            }
            
        }
        public static int GetBonusByUser(this object ID)
        {
            MLMDataContext _MLMDataContext = new MLMDataContext();
            long UserID = Permission.GetCurrentUserId();

            long RankID = long.Parse(_MLMDataContext.Users.Where(p => p.UserId == UserID).FirstOrDefault().RankID.ToString());
            var Bonus = _MLMDataContext.Ranks.Where(p => p.RankID == RankID).FirstOrDefault().Bonus;

            if (Bonus != null)
            {
                return int.Parse(Bonus.ToString());
            }
            else
            {
                return 0;
            }


        }
        public static long IdtoCity(this object id)
        {
            if (id != null)
            {
                long _id = long.Parse(id.ToString());
                using (var Datacontext = new MLMDataContext())
                {
                    return (long)Datacontext.Users.Where(p => p.UserId == _id).FirstOrDefault().CityID;
                }
            }
            else return -1;
        }

        public static string IdtoName2(this object id)
        {
            if (id != null)
            {
                long _id = long.Parse(id.ToString());
                using (var Datacontext = new MLMDataContext())
                {
                    return Datacontext.Cities.Where(p => p.CityID == _id).FirstOrDefault().Name;
                }
            }
            else return null;
        }

        public static string DisplayAcess(this object Name)
        {
            var _Name = Name.ToString();
            if (HttpContext.Current.Request.QueryString["admin"] == null)
            {
                if (!Permission.IsAdmin())
                {
                    return "";
                }
                else
                {
                    return _Name;
                }
            }
            else
            {
                return _Name;
            }
        }
    }
}